package com.hmall.search.mqListener;

import com.hmall.client.ItemClient;
import com.hmall.common.dto.Item;
import com.hmall.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author liCuiPing
 * @since 2022/12/10 19:54
 */
@Component
public class ItemListener {
    @Autowired
    private SearchService searchService;


    /**
     * 监听更新上架
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "up.queue"),
            exchange = @Exchange(name = "item.topic", type = ExchangeTypes.TOPIC),
            key = "item.up"
    ))
    public void ListenItemUp(Long id) {
        searchService.saveItemByid(id);
    }
    /**
     * 监听更新下架
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "down.queue"),
            exchange = @Exchange(name = "item.topic", type = ExchangeTypes.TOPIC),
            key = "item.down"
    ))
    public void ListenItemDown(Long id) {
        searchService.deleteItemByid(id);
    }
    /**
     * 监听删除
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delete.queue"),
            exchange = @Exchange(name = "item.topic", type = ExchangeTypes.TOPIC),
            key = "item.delete"
    ))
    public void ListenItemDelete(Long id) {
        searchService.deleteItemByid(id);
    }

    /**
     * 监听更新
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "update.queue"),
            exchange = @Exchange(name = "item.topic", type = ExchangeTypes.TOPIC),
            key = "item.update"
    ))
    public void ListenItemUpdate(Long id) {
        searchService.saveItemByid(id);
    }

    /**
     * 监听新增
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "insert.queue"),
            exchange = @Exchange(name = "item.topic", type = ExchangeTypes.TOPIC),
            key = "item.insert"
    ))
    public void ListenItemInsert(Long id) {
        searchService.saveItemByid(id);
    }
}